﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Uco.Models;
using Uco.Infrastructure;
using System.Globalization;

namespace Uco.Controllers
{
    public class PagePartController : BaseController
    {
        #region ChildAction

        [ChildActionOnly]
        [OutputCache(Duration = 3600, VaryByCustom = "LangCodeAndCleanCacheGuid")]
        public ActionResult _Footer()
        {
            object r = SF.GetSettingsRepository().Footer;
            return View(r);
        }

        [ChildActionOnly]
        [OutputCache(Duration = 3600, VaryByCustom = "LangCodeAndCleanCacheGuid")]
        public ActionResult _FooterScripts()
        {
            object r = SF.GetSettingsRepository().FotterHtml;
            return View(r);
        }

        [ChildActionOnly]
        [OutputCache(Duration = 3600, VaryByCustom = "LangCodeAndCleanCacheGuid")]
        public ActionResult _HeaderScripts()
        {
            object r = SF.GetSettingsRepository().HeaderHtml;
            return View(r);
        }

        [ChildActionOnly]
        [OutputCache(Duration = 3600, VaryByCustom = "LangCodeAndCleanCacheGuid")]
        public ActionResult _TextComponent(string SystemName)
        {
            object r = SF.GetTextComponent(SystemName);
            return View(r);
        }

        [ChildActionOnly]
        public ActionResult _Menu(int maxLevel, int? CurrentPageID)
        {
            if (CurrentPageID == null)
            {
                object r = SF.GetMenuFormated(maxLevel);
                return View(r);
            }
            else
            {
                object r = SF.GetMenuFormated(maxLevel, (int)CurrentPageID);
                return View(r);
            }
            
        }

        [ChildActionOnly]
        public ActionResult _SubMenu(int startLevel, int maxLevel, int CurentPageID)
        {
            object r = SF.GetSubMenuFormated(startLevel, maxLevel, CurentPageID);
            return View(r);
        }

        #endregion

        #region Ajax

        [HttpPost]
        public ActionResult _SmallFormAdd(ContactSmallForm csf, string InvisibleCaptchaValue)
        {
            if (!CaptchaController.IsInvisibleCaptchaValid(InvisibleCaptchaValue))
            {
                ModelState.AddModelError(string.Empty, "Captcha error.");
                SF.LogError("_SmallFormAdd Captcha error");
                return Content("Error: Captcha", "text/html");
            }

            if (ModelState.IsValid)
            {
                Contact c = new Contact();

                c.ContactEmail = csf.ContactEmail;
                c.ContactName = csf.ContactName;
                c.ContactPhone = csf.ContactPhone;

                c.ContactReferal = SF.GetCookie("Referal");
                c.ContactUrl = "<a target='_blank' href='" + Request.UrlReferrer.ToString() + "'>" + Request.UrlReferrer.ToString() + "</a>";
                c.ContactDate = DateTime.Now;

                List<Contact> cl = SF.LoadContacts();
                if (cl.Count == 0) c.ID = 1;
                else c.ID = cl.Max(r => r.ID) + 1;
                cl.Add(c);
                try { SF.SaveContacts(cl); }
                catch { }

                foreach (string item in SF.GetSettingsRepository().AdminEmail.Split(','))
                {
                    if (!SF.isEmail(item.Trim())) continue;

                    SendMail.Send(
                        item.Trim(),
                        Uco.Content.Resources.SystemModels.ContactMailSubject
                            .Replace("{ID}", c.ID.ToString())
                            .Replace("{HTTP_HOST}", Request.ServerVariables["HTTP_HOST"]),
                        Uco.Content.Resources.SystemModels.ContactMailBody
                            .Replace("{ID}", c.ID.ToString())
                            .Replace("{HTTP_HOST}", Request.ServerVariables["HTTP_HOST"])
                            .Replace("{ContactName}", c.ContactName)
                            .Replace("{ContactEmail}", c.ContactEmail)
                            .Replace("{ContactPhone}", c.ContactPhone)
                            .Replace("{ContactData}", c.ContactData)
                            .Replace("{ContactReferal}", c.ContactReferal)
                            .Replace("{ContactUrl}", c.ContactUrl)
                     );
                }

                return Content(SF.GetTextComponent("_SmallFormAdd"), "text/html");
            }
            else
            {
                return Content("Error: Model not valid", "text/html");
            }
        }

        #endregion
    }
}
